package com.example.dllo.keepdemo.dynamic;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.example.dllo.keepdemo.R;
import com.example.dllo.keepdemo.dynamic.bean.DynamicDetailsBean;
import com.example.dllo.keepdemo.tools.CalculateTimeDifference;
import com.example.dllo.keepdemo.tools.RoundImageView;
import com.example.dllo.keepdemo.tools.VolleySingleton;

/**
 * Created by dllo on 16/1/16.
 */
public class DynamicDetailsListViewAdapter extends BaseAdapter {
    private Context context;
    private DynamicDetailsBean data;

    public DynamicDetailsListViewAdapter(Context context,DynamicDetailsBean data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.getData().getCommentsList().size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_listview_activity_dynamicdetails,parent,false);
            myViewHolder = new MyViewHolder(convertView);
            convertView.setTag(myViewHolder);
        } else {
            myViewHolder = (MyViewHolder) convertView.getTag();
        }

        myViewHolder.tvName.setText(data.getData().getCommentsList().get(position).getAuthor().getUsername());
        myViewHolder.tvContent.setText(data.getData().getCommentsList().get(position).getContent());
        myViewHolder.tvTime.setText(CalculateTimeDifference.timeDifferenceUTC(data.getData().getCommentsList().get(position).getCreated()));

        if (data.getData().getCommentsList().get(position).getAuthor().getAvatar().equals("") || !(data.getData().getCommentsList().get(position).getAuthor().getAvatar().endsWith(".jpg"))) {
            myViewHolder.imgHead.setImageResource(R.mipmap.person_45_45);
        } else {
            ImageLoader.ImageListener listener = ImageLoader.getImageListener(myViewHolder.imgHead,R.mipmap.person_45_45,R.mipmap.person_45_45);
            VolleySingleton.getImageLoader().get(data.getData().getCommentsList().get(position).getAuthor().getAvatar(),listener);
        }
        return convertView;
    }

    class MyViewHolder {
        private RoundImageView imgHead;
        private TextView tvName,tvTime,tvContent;
        public MyViewHolder(View view) {
            imgHead = (RoundImageView) view.findViewById(R.id.img_item_listview_activity_dynamicdetails_headpicture);
            tvName = (TextView) view.findViewById(R.id.tv_item_listview_activity_dynamicdetails_name);
            tvTime = (TextView) view.findViewById(R.id.tv_item_listview_activity_dynamicdetails_subtitle);
            tvContent = (TextView) view.findViewById(R.id.tv_item_listview_activity_dynamicdetails_content);
        }
    }
}
